import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wu_you/l10n/messages.dart';
import 'package:wu_you/model/user/trad_model.dart';
import 'package:wu_you/page/trad/page_trad_main.dart';
import 'package:wu_you/page/trad/page_trad_payment.dart';
import 'package:wu_you/res/colors.dart';
import 'package:wu_you/state/bloc/http_bloc.dart';
import 'package:wu_you/state/bloc/trad_bloc.dart';
import 'package:wu_you/utils/extension_utils.dart';
import 'package:wu_you/utils/navigator_util.dart';
import 'package:wu_you/utils/utils.dart';
import 'package:wu_you/widgets/http_list_widget.dart';

///交易区 产品列表
class TradTypePage extends StatefulWidget{
  @override
  TradTypePageState createState() => TradTypePageState();
}
class TradTypePageState extends State<TradTypePage>{
  var message;
  @override
  Widget build(BuildContext context) {
    message = AppLocalizations.of(context);
    return Scaffold(
      appBar: AppBar(
        title: Text("${message['trad5']}"),
      ),
      body: Column(
        children: [
          Container(
            height: 62,
            margin: EdgeInsets.all(12),
            width: double.infinity,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(6)),
              color: Colors.white,
            ),
            padding: EdgeInsets.only(left: 12,right: 12),
            child: Row(
              children: [
                Expanded(child: Container(child: Text("${message['trad1']}",style: TextStyle(color: AppColors.textHintColor),),alignment: Alignment.centerLeft,)),
                Expanded(child: Container(child: Text("${message['trad2']}",style: TextStyle(color: AppColors.textHintColor),),alignment: Alignment.center,)),
                Expanded(child: Container(child: Text("${message['trad3']}",style: TextStyle(color: AppColors.textHintColor),),alignment: Alignment.center,)),
                Expanded(child: Container(child: Text("${message['trad4']}",style: TextStyle(color: AppColors.whiteColor),),alignment: Alignment.center,)),
              ],
            ),
          ),


          Expanded(
              child: HttpListWidget<TradTypeModel, TradTypesBloc>(
                  creator: (context) => TradTypesBloc(),
                  onObtainData: (context, page) {
                    BlocProvider.of<TradTypesBloc>(context).add(PageSizeHttpEvent(1,100));
                  },
                  successBuilder: (context, data) {
                    return ListView.builder(
                      itemBuilder: (context,index){
                        TradTypeModel item = data[index];
                        return Container(
                          height: 62,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.all(Radius.circular(6)),
                            color: Colors.white,
                          ),
                          margin: EdgeInsets.only(left: 12,right: 12,bottom: 12),
                          padding: EdgeInsets.only(left: 12,right: 12),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Expanded(child: Container(
                                child: Column(
                                  children: [
                                    Text("${item.name}",style: TextStyle(color: AppColors.textColor,fontWeight: FontWeight.bold),),
                                    // Image.network("${item.path}",width: 22,height: 22,fit: BoxFit.fill,errorBuilder: (context,obj,stack){
                                    //   return Image.asset(Utils.getImgPath('ic_logo'),width: 22,height: 22,fit: BoxFit.fill,);
                                    // },)
                                  ],
                                  mainAxisSize: MainAxisSize.min,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                )
                                ,alignment: Alignment.centerLeft,)
                              ),
                              Expanded(child: Container(child: Text("${item.consult_price}",style: TextStyle(color: AppColors.textColor),),alignment: Alignment.center,)),
                              Expanded(child: Container(child: Text("${item.ratio}",style: TextStyle(color: _setRadioColor(item.ratio)),),alignment: Alignment.center,)),
                              Expanded(
                                child: Container(
                                  child: Container(
                                  decoration: BoxDecoration(
                                    color: AppColors.primaryColor,
                                    borderRadius: BorderRadius.circular(13),
                                  ),
                                  padding: EdgeInsets.only(left: 8,right: 8,top: 4,bottom: 4),
                                  child: Text("${message['trad4']}",style: TextStyle(color: AppColors.whiteColor,fontSize: 13),),
                                ).addGesture(() {
                                  NavigatorUtil.navigateToPage(context, TradingMainPage(model: item,));
                                }),
                                alignment: Alignment.center,)
                              ),
                            ],
                          ),
                        );
                      },
                      itemCount: data.length,
                    );
                  }
              )
          )
        ],
      ),
    );
  }


  Color _setRadioColor(String radio){
    if(radio.startsWith('-')){
      return Colors.green;
    }else{
      if(radio.startsWith('0.00')){
        return Colors.green;
      }else{
        return AppColors.primaryColor;
      }
    }
  }
}